import docx
import re
from docx.parts.image import ImagePart
import os
import shutil
# doc=docx.Document(r"D:\code\pythonProject\test\Wordfiles\分光光度法分析水中污染物原始记录-总磷GB11893-89.docx")
# p1=doc.paragraphs[1]
# print(p1.text)
# print(p1.style.name)
# print(p1.runs)
# r1=p1.runs[0]
# print(r1.text)
# print(r1.bold)
# print(r1.font.color.rgb)

# ps=doc.paragraphs
# for p in ps:
#     print(p.style.name,p.text)

# table=doc.tables
# for data in table:
#     print(data)
#
# for p in doc.paragraphs:
#     print(p._p.xml)
#     if "Graphic" in p._p.xml:
#         id=re.findall('<a:blip r:embed="(\w+)"',p._p.xml)[0]
#         print(id)
#     else:
#         print(p.text)

#获取所有图片ID和图片路径的对应关系
# image_rel_dict ={}
# for item in doc.part.rels.values():
#     # print(item)
#     if type(item.target_part)==ImagePart:
#         # print(item.target_part)
#         # print(item.rId,item.target_part.partname)
#         image_rel_dict[item.rId]=item.target_part.partname
# # print(image_rel_dict)
#
# # 读取word文件
# for p in doc.paragraphs:
#     if "Graphic" in p._p.xml:
#         # print(p._p.xml)
#         image_id=re.findall('<a:blip r:embed="(\w+)"',p._p.xml)[0]
#
#         # print("图片ID=",image_id)
#         image_path=image_rel_dict[image_id]
#         print(image_path)
#     else:
#         print(p.text)

file_path =os.path.join(r"D:\code\pythonProject\test\Wordfiles","分光光度法分析水中污染物原始记录-总磷GB11893-89.docx")
zip_file_path=os.path.join(r"D:\code\pythonProject\test\Wordfiles","分光光度法分析水中污染物原始记录-总磷GB11893-89.zip")
shutil.copy(file_path,zip_file_path)
target_folder=os.path.join(r"D:\code\pythonProject\test\Wordfiles","总磷")
shutil.unpack_archive(zip_file_path,target_folder,format="zip")
def zip_folder(folder_path,output_zip):
    if not os.path.exists(folder_path):
        print(f"文件夹{folder_path}不存在")
        return
    shutil.make_archive(output_zip,"zip",folder_path)
    print(f"文件夹{folder_path}压缩为{output_zip}.zip成功")
zip_folder(r"D:\code\pythonProject\test\files","F:\压缩文件")